//初始化依赖
layui.use(['table','form','layer','jquery'], function(){
    var table = layui.table, //表格组件
        layer = layui.layer, //弹出层组件
        form = layui.form, //表单组件
        $lay = layui.jquery; //jquery
    var baseUrl = $.common.baseUrl();

    //加载表格
    table.render({
         elem: '#mtab'
        ,url: '/system/permission' //数据接口
        ,method:'get'
        ,toolbar:'#toolbar'
        ,page: true //开启分页
        ,cols: [[ //表头
             {field: 'checkbox', title: '选择',fixed: 'left',type:'checkbox'}
            ,{field: 'id', title: 'ID', fixed: 'left'}
            ,{field: 'name', title: '权限名称'}
            ,{field: 'description', title: '描述'}
            ,{field: 'createDate', title: '创建时间',sort: true}
            ,{field: 'updateDate', title: '更新时间',sort: true}
            ,{field: 'info', title: '操作', templet:'#bar',fixed: 'right'}
        ]]
    });

    //列表顶部工具栏操作
    table.on('toolbar(mtab)', function(obj){
        var checkStatus = table.checkStatus(obj.config.id);
        var data = checkStatus.data;

        switch(obj.event){
            case 'add':
                layer.open({
                    title: '新增',
                    type:2,//iframe
                    area: ['750px', '550px'],
                    maxmin:true,
                    btn:['关闭'],
                    yes:function(index,layero){
                       layer.close(index);
                    },
                    content: 'add'
                });
                break;
            case 'del':
                var ids = new Array();
                if(data != null && data.length > 0){
                    $lay(data).each(function(){
                        ids.push(this.id);
                    })
                }
                if(ids == null || ids.length == 0){
                    layer.msg('请勾选需要删除的行!');
                    return;
                }

                $.ajax({
                    url: "/system/permission",
                    type: "post",
                    dataType: "json",
                    data:{_method:"DELETE",ids:ids.join(",")},
                    traditional: true,
                    async: true,//异步,
                    success: function(result){
                        if(result.errorCode == "suc"){
                            table.reload('mtab');
                        }

                    },error:function(){

                    }
                });
                break;
            case 'edit':
                break;
        };
    });

    //监听表格复选框选择
    table.on('checkbox(mtab)', function(obj){
        console.log(obj)
    });
    //监听工具条
    table.on('tool(mtab)', function(obj){
        var data = obj.data;
        if(obj.event === 'detail'){
            layer.open({
                title: '查看',
                type:2,//iframe
                area: ['750px', '550px'],
                maxmin:true,
                btn:['关闭'],
                yes:function(index,layero){
                    layer.close(index);
                },
                content: 'view/' + data.id
            });
        } else if(obj.event === 'del'){
            layer.confirm('真的删除行么', function(index){
                $.ajax({
                    url: "/system/permission",
                    type: "post",
                    dataType: "json",
                    data:{_method:"DELETE",ids:data.id},
                    traditional: true,
                    async: true,//异步,
                    success: function(result){
                        if(result.errorCode == "suc"){
                            layer.close(index);
                            table.reload('mtab');
                        }

                    },error:function(){

                    }
                });

            });
        } else if(obj.event === 'edit'){
            layer.open({
                title: '编辑',
                type:2,//iframe
                area: ['750px', '550px'],
                maxmin:true,
                btn:['关闭'],
                yes:function(index,layero){
                    layer.close(index);
                },
                /*end:function(){
                    //重载表格
                    table.reload('mtab');
                },*/
                content: 'edit/' + data.id
            });
        }
    });

    active = {
        getCheckData: function(){ //获取选中数据
            var checkStatus = table.checkStatus('idTest')
                ,data = checkStatus.data;
            layer.alert(JSON.stringify(data));
        }
        ,getCheckLength: function(){ //获取选中数目
            var checkStatus = table.checkStatus('idTest')
                ,data = checkStatus.data;
            layer.msg('选中了：'+ data.length + ' 个');
        }
        ,isAll: function(){ //验证是否全选
            var checkStatus = table.checkStatus('idTest');
            layer.msg(checkStatus.isAll ? '全选': '未全选')
        }
    };

    $('.demoTable .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
});




